package com.budwk.app.util.poiUtils.fireHiddenOffice;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.text.StrFormatter;
import com.budwk.app.sqfw.enums.StreetEnum;
import com.budwk.app.sqfw.enums.WeiQingMenPdfType;
import com.budwk.app.sqfw.models.Sqfw_form_summary_pdf;
import com.budwk.app.sqfw.services.SqfwFormSummaryPdfService;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author XYD-PC021
 * @project wk-mini
 * @description 生成指定的pdf
 * @date 2023/12/1 10:43:05
 */
public class GeneratePdfConverge {

    private static SqfwFormSummaryPdfService formSummaryPdfService =
            (SqfwFormSummaryPdfService) NutzIocUtils.getBeanByType(SqfwFormSummaryPdfService.class);

    /**
     * pdf下载
     * @param unitName
     * @param ids
     * @param index
     * @param isCheckData
     * @return
     */
    public static String downPdfConverge(String unitName, List<String> ids, int index, String ... isCheckData) {
        if (unitName.contains(StreetEnum.WEI_QING_MEN_STREET.getName())) {

            List<Integer> integers = WeiQingMenPdfType.getIdx(index);

            Cnd cnd = Cnd.NEW();
            cnd.and("summary_id", "in", ids)
                    .and("index", "in", integers)
                    .and("status", "=", "2")
                    .orderBy("create_date", "asc")
                    .orderBy("index", "asc");
            List<Sqfw_form_summary_pdf> pdfList = formSummaryPdfService.query(cnd);

            // pdf是否有数据检查
            if (isCheckData.length > 0 && "isCheckData".equals(isCheckData[0])) {
                if (pdfList != null && pdfList.size() > 0) {
                    return "true";
                } else {
                    return "false";
                }
            }

            List<String> urls = pdfList.stream().map(Sqfw_form_summary_pdf::getUrl).collect(Collectors.toList());

            String name = WeiQingMenPdfType.getName(index);
            if (name == null) return null;

            String fileName = StrFormatter.format("{}-{}", name, DateUtil.format(new Date(), "yyyyMMdd"));
            String url = CommonFormOfficeUtils.createConvergePdf(urls, fileName);
            if (StringUtils.isBlank(url)) {
                return null;
            }
            if (url.startsWith("/")) return NutzIocUtils.getOssPrefix() + url;
            return url;
        }
        return null;
    }
}
